﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Networking
{
    /// <summary>
    /// Packet of type PacketSecurity, the packet that contains the supported security types
    /// </summary>
    public class SecurityPacket : Packet
    {

        /// <summary>
        /// Array of supported security types
        /// </summary>
        private SecurityType[] Supported;

        /// <summary>
        /// If a provided security type is supported
        /// </summary>
        /// <param name="Security">The security type to check</param>
        /// <returns>True if the security type is supported, False otherwise</returns>
        public bool IsSupported(SecurityType Security)
        {
            return Supported.Any(item => item == Security);
        }

        /// <summary>
        /// Returns a copy of the array of all the supported security types
        /// </summary>
        /// <returns>Copy of array of all the supported seciurity types</returns>
        public SecurityType[] GetSupported()
        {
            return (SecurityType[]) Supported.Clone();
        }

        /// <summary>
        /// Creates a new instance of type PacketSecurity
        /// </summary>
        /// <param name="Supported">The array of all supported security types</param>
        public SecurityPacket(SecurityType[] Supported)
            : base(PacketType.Security)
        {
            this.Supported = new SecurityType[Supported.Count()];
            Supported.CopyTo(Supported, 0);
        }

        /// <summary>
        /// Creates a new instance of SecurityPacket extracting all the parameters from the byte buffer
        /// </summary>
        /// <param name="buffer">The byte buffer containing the packet data</param>
        public SecurityPacket(byte[] buffer)
            : base(PacketType.Security, buffer)
        {
            //
        }

        /// <summary>
        /// Serializes the packet in order to send it
        /// </summary>
        /// <returns>The bytes of the packet</returns>
        public override byte[] Serialize()
        {

            byte[] buffer = new byte[8 * (1 + Supported.Count())];

            buffer[0] = (byte)Supported.Count();

            for (int n = 0; n < Supported.Count(); n++)
            {
                buffer[n + 1] = (byte)Supported[n];
            }

            return buffer;


        }



        /// <summary>
        /// Loads a packet from a byte buffer
        /// </summary>
        /// <param name="buffer">The data buffer</param>
        public override void Load(byte[] buffer)
        {

            //if (buffer.Length != 10) throw new BufferSizeMismatchException(10);

            MemoryStream memory = new MemoryStream(buffer);

            BinaryReader reader = new BinaryReader(memory);

            int iCount = reader.ReadByte();

            if (buffer.Length != iCount + 1) 
                throw new CorruptedPacketException(PacketType.Security, "The number of securities provided doesnt match the count");

            this.Supported = new SecurityType[iCount];

            //this.Type = (PacketType)reader.ReadByte();

            //if (this.Type != PacketType.FramebufferUpdateRequest)
            //    throw new PacketTypeMismatchException(PacketType.FramebufferUpdateRequest, this.Type);

            for (int n = 0; n < iCount; n++)
            {
                this.Supported[n] = (SecurityType)reader.ReadByte();
            }
            
            reader.Close();
        
        }
    }
}
